﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using Yipusoft.Common.BLL;
using Yipusoft.Common.Model;
using System.Collections.Generic;

public partial class System_UserNew : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["UserID"] == null)
            {
                ActionTitle.Text = "添加人员";
            }
            else
            {
                ActionTitle.Text = "编辑人员";
                BindData();
                UserID.ReadOnly = true;
            }
        }
    }
    private void BindData()
    {
        UserInfo user = Yipusoft.Account.BLL.User.GetUser(Request.QueryString["UserID"]);
        UserID.Text = user.UserID;
        CascadingDropDown_Department.SelectedValue = user.DepartmentID.Substring(0, 4);
        CascadingDropDown_Unit.SelectedValue = user.DepartmentID;
        UserName.Text = user.UserName;
        Password.Text = user.Password;
        StateList.SelectedIndex = StateList.Items.IndexOf(StateList.Items.FindByValue(user.State.ToString()));
    }
    private void Insert()
    {
        if (Yipusoft.Account.BLL.User.Exsits(UserID.Text))
        {
            Prompt.Text = "警告：该人员编码已存在！";
            return;
        }
        UserInfo user = new UserInfo();
        user.UserID = UserID.Text;
        user.UserName = UserName.Text;
        user.Password = Password.Text;
        user.DepartmentID = UnitList.SelectedValue;
        user.TimeLastLogin = DateTime.Now;
        user.IPLastLogin = "";
        user.TimeRegister = DateTime.Now;
        user.State = int.Parse(StateList.SelectedValue);
        Yipusoft.Account.BLL.User.Insert(user);
        Yipusoft.Account.BLL.User.AddRole(UserID.Text, "999999");

        EventLogInfo log = new EventLogInfo();
        log.EventTime = DateTime.Now;
        log.EventType = 1;
        log.EventData = "添加了一个人员";
        log.ItemID = UserID.Text;
        log.ItemName = "人员信息表";
        log.ItemFullUrl = "UserNew.aspx";
        log.CreatedBy = Context.User.Identity.Name;
        log.CreatedTime = DateTime.Now;
        log.UserIP = Request.UserHostAddress;
        EventLog.Insert(log);

        Prompt.Text = "提示：添加成功！";
    }
    private void Update()
    {
        UserInfo user = Yipusoft.Account.BLL.User.GetUser(Request.QueryString["UserID"]);
        user.UserName = UserName.Text;
        user.Password = Password.Text;
        user.DepartmentID = UnitList.SelectedValue;
        user.TimeLastLogin = DateTime.Now;
        user.IPLastLogin = "";
        user.TimeRegister = DateTime.Now;
        user.State = int.Parse(StateList.SelectedValue);
        Yipusoft.Account.BLL.User.Update(user);

        EventLogInfo log = new EventLogInfo();
        log.EventTime = DateTime.Now;
        log.EventType = 1;
        log.EventData = "更新了一个人员";
        log.ItemID = UserID.Text;
        log.ItemName = "人员信息表";
        log.ItemFullUrl = "UserNew.aspx";
        log.CreatedBy = Context.User.Identity.Name;
        log.CreatedTime = DateTime.Now;
        log.UserIP = Request.UserHostAddress;
        EventLog.Insert(log);

        Prompt.Text = "提示：更新成功！";
    }
    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["UserID"] == null)
            Insert();
        else
            Update();
    }
}
